from flask import Blueprint, request

from app.api.v1.controller.base import Base
from app.api.v1.service.knowledge_service import KnowledgeService
from zhanshop.app import App
from zhanshop.validate import Validate

v1_knowledge = Blueprint("v1_knowledge", __name__, url_prefix='/v1/knowledge')
class Knowledge(Base):
    @v1_knowledge.route('/repository', methods=["GET"])
    @staticmethod
    def getRepository():
        """
        @api GET repository 知识管理
        @apiParam string database 知识库数据库名
        @apiParam string table 知识库表名
        @apiParam string question 需要搜索的问题
        @apiParam int limit 结果最大返回数
        @apiParam json model= 模型参数更多参数参考：chromadb->https://docs.trychroma.com/docs/overview/getting-started,SentenceTransformer->https://sbert.net/docs/sentence_transformer/usage/usage.html
        @apiSuccess string data 搜索后的结果
        @apiGroup 向量库
        @apiDescription 搜索一个知识库
        @return:
        """
        inputData = Validate(request, {
            "database": "required | string",
            "table": "required | string",
            "question": "required | string",
            "limit": "required | int",
            "model": "json"
        }).getData()
        result = App.make(KnowledgeService).getRepository(inputData["database"], inputData["table"], inputData["question"], inputData["limit"], inputData["model"])
        return Knowledge.result(result)
    @v1_knowledge.route('/repository', methods=["POST"])
    @staticmethod
    def postRepository():
        """
        @api POST repository 添加知识
        @apiParam string database 知识库数据库名
        @apiParam string table 知识库表名
        @apiParam json model= 模型参数更多参数参考：chromadb->https://docs.trychroma.com/docs/overview/getting-started,SentenceTransformer->https://sbert.net/docs/sentence_transformer/usage/usage.html
        @apiParam array documents 知识库数据列表
        @apiParam array documents 知识库数据列表
        @apiParam array metadatas= 内容相关源数据
        @apiSuccess string data 添加后的结果
        @apiGroup 向量库
        @apiDescription 添加一个知识库
        @return:
        """
        inputData = Validate(request, {
            "database": "required | string",
            "table": "required | string",
            "documents": "required | array",
            "metadatas": "array",
            "model": "json"
        }).getData()
        result = App.make(KnowledgeService).postRepository(inputData["database"], inputData["table"], inputData["documents"], inputData["metadatas"], inputData["model"])
        return Knowledge.result(result)
